#!/bin/bash
######################################################################
# this script is used for extracting tomcat accesslog parameters into 
# sepcificed csv file
######################################################################

startday=$1
endday=$2

for ((day=$startday; day<=$endday; day++))
do
    echo "[info] downloading day $day's log ..."
    wget ftp://iptv_vis_logbmm:iptv_vis_logbmm@124.108.10.62/Guangzhou/logbmm_201303$day*.zip
    echo "unziping day $day's log... "
    unzip logbmm_201303$day\*.zip
    cat *.log >> logbmm_201303$day.merged
    rm -f *.log
done


echo '[info] converting to csv file ...'
awk -F'?' '{print $2}' logbmm_201303*.merged |
awk -F' - FIRST_UP' '{print $1}' |
awk '{split($0,arr,"&"); print(substr(arr[1],index(arr[1],"=")+1) "," substr(arr[2],index(arr[2],"=")+1) "," substr(arr[3],index(arr[3],"=")+1) "," substr(arr[4],index(arr[4],"=")+1) "," strftime("%Y-%m-%d %H:%M:%S",substr(arr[5],index(arr[5],"=")+1,10)) "," substr(arr[6],index(arr[6],"=")+1) )} ' > clicklog.csv

